<?php
if(!$is_success){
    $dp = new jDatePicker();
    $dp->__dateFormat = "yy-mm-dd";
    echo "<scr"."ipt type=\"text/javascript\">";
    $dp->generateDP("tanggal_perjanjian");
    $dp->generateDP("kesediaan_muat");
    echo "</scr"."ipt>";
    $dp->init();
?>
<!--<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">-->
<style>
    .shou{
        display: none;
    }
</style>
<!--<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>-->
<script>    
    function _obj_fSendValue(kode, nama, elmKode, elmNama) {
            var elmk = document.getElementById("" + elmKode + "");
            var elmn = document.getElementById("" + elmNama + "");
            try {
                    elmk.value = kode;
                    elmn.value = nama;
                    elmn.setAttribute("readonly", true);
                    jQuery(elmn).addClass("read-only");
                    
                    jQuery.ajaxSetup({async: false});
                    jQuery.ajax({
                        type: "POST",
                        url : "<?php echo site_url('popup/get_customer_detil') ?>",
                        data: {kode: kode},
                        async: false,
                        cache: false,
                        crossDomain: true,
                        success: function(msg){
                            var result = jQuery.parseJSON(msg);

                            jQuery("#alamat_detail_customer").val(result.alamat_detail_customer);
                            jQuery("#alamat_komplit").val(result.alamat_komplit);
                            jQuery("#kontak_komplit").val(result.kontak_komplit);
                            jQuery("#attendance").val(result.attendance);
//                            jQuery("#hp_detail").val(result.no_hp);
                        },
                        error: function(msg){
                            alert(msg);
                        }
                    }); 
                    jQuery.ajaxSetup({async: true});
            }
            catch(e) {
                    alert(e.message);
            }
    }
        
    function _obj_fSendVal(kode, nama, elmKode, elmNama) {
            var elmk = document.getElementById("" + elmKode + "");
            var elmn = document.getElementById("" + elmNama + "");
            try {
                    elmk.value = kode; 
                    elmn.value = nama; 
                    elmn.readonly = true;
                    jQuery(elmn).addClass("read-only");
            }
            catch(e) {
                    alert(e.message);
            }
    }
    
    function clear_element(elm){
        jQuery("#nama_"+elm).removeClass("read-only");
        jQuery("#nama_"+elm).attr("readonly", false);
        jQuery("#nama_"+elm).val("");
        jQuery("#id_"+elm).val("");
    }
    
    function isUsePPN(){
        var use = document.getElementById("ppn").checked;
        if(use){
            return true;
        }else{
            return false;
        }
    }
    
    function setBlnToRome(bln){
        if(bln=="01"){
            return "I";
        }else if(bln=="02"){
            return "II";
        }else if(bln=="03"){
            return "III";
        }else if(bln=="04"){
            return "IV";
        }else if(bln=="05"){
            return "V";
        }else if(bln=="06"){
            return "VI";
        }else if(bln=="07"){
            return "VII";
        }else if(bln=="08"){
            return "VIII";
        }else if(bln=="09"){
            return "IX";
        }else if(bln=="10"){
            return "X";
        }else if(bln=="11"){
            return "XI";
        }else{
            return "XII";
        }
    }
    
    function setPerjanjianNumber(){
         var prefix = <?php echo json_encode($prefix); ?>;

         var date = (jQuery("#tanggal_perjanjian").val()).toString();
         var partsArray = date.split('-');
         var tgl = partsArray[2];
         var bln = setBlnToRome(partsArray[1]);
         var thn =  partsArray[0];
         var no= prefix + "/SPAL/ASP/" + tgl +"/" + bln +"/" +thn;
         jQuery("#nomor_perjanjian").val(no);
         
    }
   
    function close_gb(){
        <?php echo CloseGB(false); // close greybox   ?>
    }
    
    function close_gb_2(){
        <?php echo CloseGB(true); // close greybox   ?>
    }
    
    function formatNumber (obj, decimal) {
        //decimal  - the number of decimals after the digit from 0 to 3
        //-- Returns the passed number as a string in the xxx,xxx.xx format.
        anynum = eval(obj);
        divider =10;
        switch(decimal){
            case 0:
                divider =1;
                break;
            case 1:
                divider =10;
                break;
            case 2:
                divider =100;
                break;
            default:       //for 3 decimal places
                divider =1000;
        }

        workNum=Math.abs((Math.round(anynum*divider)/divider));
        workStr=""+workNum
        if (workStr.indexOf(",")==-1){workStr+=","}
        dStr=workStr.substr(0,workStr.indexOf(","));
        dNum=dStr-0
        pStr=workStr.substr(workStr.indexOf(","))

        while (pStr.length-1< decimal){
            pStr+="0"
        }

        if(pStr ==',') pStr ='';
        //--- Adds a comma in the thousands place.
        if (dNum>=1000) {
            dLen=dStr.length
            dStr=parseInt(""+(dNum/1000))+"."+dStr.substring(dLen-3,dLen)
        }
        //-- Adds a comma in the millions place.
        if (dNum>=1000000) {
            dLen=dStr.length
            dStr=parseInt(""+(dNum/1000000))+"."+dStr.substring(dLen-7,dLen)
        }

        retval = dStr + pStr
        //-- Put numbers in parentheses if negative.
        if (anynum<0) {retval="("+retval+")";}
        //You could include a dollar sign in the return value.
        //retval =  "$"+retval
        return retval;
    }
</script>

<h3 class="title-popup">Form Tambah Perjanjian</h3>

<div style="padding:0 10px 0 10px;">
    <?php if ($error != "") { ?>
        <div class="BoxMessage Warning">Proses Gagal, <br /><?php echo $error; ?></div>
    <?php } ?>
    
<div id="content">
<div class="DivTable">
<?= form_open() ?>
<table>
    <tr>
        <td width="5%">&nbsp;</td>
        <td width="1%">&nbsp;</td>
        <td width="35%">&nbsp;</td>
        <td width="5%">&nbsp;</td>
        <td width="8%">&nbsp;</td>
        <td width="5%">&nbsp;</td>
        <td width="1%">&nbsp;</td>
        <td width="35%">&nbsp;</td>
        <td width="5%">&nbsp;</td>
    </tr>
    <tr>
        <td>To</td>
        <td>:</td>
        <td>
            <input type="text" name="nama_customer" id="nama_customer" value="<?php echo $nama_customer;?>" maxlength="40" size="50" />
            <input type="hidden" name="id_customer" id="id_customer" value="<?php echo $id_customer;?>"   />
            <?php echo $gbcust; ?>
            <a class="clear" onclick="clear_element('customer');" title="Clear Choice"><span></span></a>
        </td>
        <td>
            &nbsp;
        </td>
        <td>&nbsp;&nbsp;&nbsp;</td>
        <td>Date</td>
        <td>:</td>
        <td><?php echo form_input(array("name" => "tanggal_perjanjian", "id" => "tanggal_perjanjian" ,"class" => "read-only", "readonly" => "readonly", "size" => "47"), $tanggal_perjanjian, "onchange=\"setPerjanjianNumber()\"");?></td>
    </tr>
    <tr>
        <td>Detail Address</td>
        <td>:</td>
        <td>
            <?php echo form_input(array("name" => "alamat_detail_customer", "id" => "alamat_detail_customer", "class" => "input_text", "size" => "50"),$alamat_detail_customer);?>
        </td>
        <td>&nbsp;</td>

        <td>&nbsp;&nbsp;&nbsp;</td>
        <td>SPAL NO</td>
        <td>:</td>
        <td>
            <?php
            echo form_input(array("name" => "nomor_perjanjian", "id" => "nomor_perjanjian", "size" => "50"),$nomor_perjanjian);
            if($nomor_perjanjian == "")
                echo "<script>setPerjanjianNumber()</script>";
            ?>
        </td>
    </tr>
    <tr>
        <td>Full Address</td>
        <td>:</td>
        <td>
            <?php echo form_textarea(array("name" => "alamat_komplit", "id" => "alamat_komplit", "class" => "input_text", "rows"=>4,"cols"=>40  ), str_replace("<br/>", "\n", $alamat_komplit));?>
        </td>
        <td>
            <a class="help" onclick="alert('Berisi alamat lengkap, kota, kodepos. Dipisahkan oleh tanda enter');" title="Petunjuk"><span></span></a>
        </td>

        <td>&nbsp;&nbsp;&nbsp;</td>
        <td>Kesedian Muat</td>
        <td>:</td>
        <td><?php echo form_input(array("name" => "kesediaan_muat", "id" => "kesediaan_muat", "class" => "input_text", "readonly" => "readonly", "size" => 47), $kesediaan_muat);?></td>      
        
    </tr>
    <tr>
        <td>Phone/Fax/Email</td>
        <td>:</td>
        <td>
            <?php echo form_input(array("name" => "kontak_komplit", "id" => "kontak_komplit", "class" => "input_text", "size"=>50  ),$kontak_komplit);?>
        </td>
        <td>
            <a class="help" onclick="alert('Berisi no telepon, fax, dan email. Dipisahkan oleh tanda /');" title="Petunjuk"><span></span></a>
        </td>
        <td>&nbsp;&nbsp;&nbsp;</td>
        <td>Asuransi Barang</td>
        <td>:</td>
        <td><?php echo form_input(array("name" => "asuransi_barang", "id" => "asuransi_barang", "size"=>50), $asuransi_barang, "");?></td>
    </tr>
    <tr>
        <td>Attendance</td>
        <td>:</td>
        <td><?php echo form_input(array("name" => "attendance", "id" => "attendance", "class" => "input_text", "size"=>50), $attendance);?></td>
        <td>&nbsp;</td>
        <td>&nbsp;&nbsp;&nbsp;</td>        
        <td>Asuransi Kapal</td>
        <td>:</td>
        <td><?php echo form_input(array("name" => "asuransi_kapal", "id" => "asuransi_kapal", "size"=>50), $asuransi_kapal, "");?></td>
    </tr>
    <tr>
        &nbsp;&nbsp;&nbsp;
    </tr>
    <tr>
        <td>Subject</td>
        <td>:</td>
        <td align="left"><?php echo form_input(array("name" => "subject", "id" => "subject", "size"=>50),$subject);?></td> 
        <td>&nbsp;</td>
        <td>&nbsp;&nbsp;&nbsp;</td> 
        <td>Pelabuhan Muat</td>
        <td>:</td>
        <td><?php echo form_input(array("name" => "pelabuhan_muat", "id" => "pelabuhan_muat", "size"=>"50"), $pelabuhan_muat);?></td>
    </tr>
    <tr>
        <td>Vessel</td>
        <td>:</td>
        <td>
            <input type="text" name="nama_vessel" id="nama_vessel" value="<?php echo $nama_vessel;?>" maxlength="40" size="50" />
            <input type="hidden" name="id_vessel" id="id_vessel" value="<?php echo $id_vessel;?>"   />
        </td>
        <td>
            <?php echo $gbvessel; ?>
            <a class="clear" onclick="clear_element('vessel');" title="Clear Choice"><span></span></a>
        </td>
    </tr>
    <tr>
        <td>Currency</td>
        <td>:</td>
        <td>
            <?php 
                if ($using_dollar_currency == 1){
                    $crc = "USD";
                } else {
                    $crc = "IDR";
                }
            ?>
            <select id="curency" name="using_dollar_currency" value="<?php echo $crc;?>">
                        <option value="<?php echo $crc;?>"><?php echo $crc;?></option>                        
                        <option value="USD">USD</option>
                        <option value="IDR">IDR</option>                        
            </select>
            <?php echo form_checkbox(array("name"=>"tambahkan_ppn","id" => "ppn"), 1, $tambahkan_ppn, "onclick=\"updateGrandTotal()\"");?> PPH
        </td>
        <td>&nbsp;</td>
        <td>&nbsp;&nbsp;&nbsp;</td>
        <td>Pelabuhan Bongkar</td>
        <td>:</td>
        <td><?php echo form_input(array("name" => "pelabuhan_bongkar", "id" => "pelabuhan_bongkar", "size"=>"50"), $pelabuhan_bongkar);?></td>
    </tr>
    <tr>
        <td>Diskon</td>
        <td>:</td>
        <td><?php echo form_input(array("name" => "diskon", "id" => "diskon", "size"=>50), $diskon, "onkeyup=\"this.value=this.value.replace(/[^\d]/,'');\"");?></td>
        <td>&nbsp;</td>
        <td>&nbsp;&nbsp;&nbsp;</td>
        <td>Jumlah Hari</td>
        <td>:</td>
        <td><?php echo form_input(array("name" => "jumlah_hari", "id" => "jumlah_hari", "size"=>"50"), $jumlah_hari);?></td>
    </tr>
    <tr>
        <td>Uang Tambang</td>
        <td>:</td>
        <td><?php echo form_input(array("name" => "uang_tambang", "id" => "uang_tambang", "size"=>50), $uang_tambang, "onkeyup=\"this.value=this.value.replace(/[^\d]/,'');\"");?></td>
        <td>&nbsp;</td>
        <td>&nbsp;&nbsp;&nbsp;</td>
        <td>Jumlah Muatan</td>
        <td>:</td>
        <td><?php echo form_input(array("name" => "jumlah_muatan", "id" => "jumlah_muatan", "size"=>"50"), $jumlah_muatan);?></td>
    </tr>

    <tr>    
        <td>Kondisi Uang Tambang</td>
        <td>:</td>
        <td><?php echo form_input(array("name" => "kondisi_uang_tambang", "id" => "kondisi_uang_tambang", "size"=>50), $kondisi_uang_tambang, "");?></td>
        <td>&nbsp;</td>
        <td>&nbsp;&nbsp;&nbsp;</td>
        <td>Pengirim Barang</td>
        <td>:</td>
        <td><?php echo form_input(array("name" => "pengirim_barang", "id" => "pengirim_barang", "size"=>"50"), $pengirim_barang);?></td>
    </tr>
    <tr>
         <td>Bank</td>
        <td>:</td>
        <td>
            <input class="read-only" type="text" name="nama_bank" id="nama_bank" value="<?php echo $nama_bank;?>" maxlength="40" size="50" readonly="readonly" />
        <input type="hidden" name="id_bank" id="id_bank" value="<?php echo $id_bank;?>" />  
            <?php 
            echo $gbbank;?>
        <a class="clear" onclick="clear_element('bank');" title="Clear Choice"><span></span></a>
        </td>
        <td>&nbsp;</td>
        <td>&nbsp;&nbsp;&nbsp;</td>
        <td>Penerima Barang</td>
        <td>:</td>
        <td><?php echo form_input(array("name" => "penerima_barang", "id" => "penerima_barang", "size"=>"50"), $penerima_barang);?></td>
    </tr>
    <tr>
        <td>Jenis Muatan</td>
        <td>:</td>
        <td>
            <?php echo form_textarea(array("name" => "jenis_muatan", "id" => "jenis_muatan", "class" => "input_text", "rows"=>4,"cols"=>40  ), str_replace("<br/>", "\n", $jenis_muatan));?>
        </td>
        <td>&nbsp;</td>
        <td>&nbsp;&nbsp;&nbsp;</td>
        <td>Cara Pembayaran</td>
        <td>:</td>
        <td>
            <?php echo form_textarea(array("name" => "cara_pembayaran", "id" => "cara_pembayaran", "class" => "input_text", "rows"=>4,"cols"=>40  ), str_replace("<br/>", "\n", $cara_pembayaran));?>
        </td>
    </tr>
    <tr>
        <td>Kondisi Kontrak Muatan</td>
        <td>:</td>
        <td>
            <?php echo form_textarea(array("name" => "kondisi_kontrak", "id" => "kondisi_kontrak", "class" => "input_text", "rows"=>4,"cols"=>40  ), str_replace("<br/>", "\n", $kondisi_kontrak));?>
        </td>
        <td>&nbsp;</td>
        <td>&nbsp;&nbsp;&nbsp;</td>
        <td>Denda Keterlambatan</td>
        <td>:</td>
        <td>
            <?php echo form_textarea(array("name" => "denda_keterlambatan", "id" => "denda_keterlambatan", "class" => "input_text", "rows"=>4,"cols"=>40  ), str_replace("<br/>", "\n", $denda_keterlambatan));?>
        </td>
    </tr>
    <tr>
        
        <td>Keagenan Kapal</td>
        <td>:</td>
        <td>
            <?php echo form_textarea(array("name" => "keagenan_kapal", "id" => "keagenan_kapal", "class" => "input_text", "rows"=>4,"cols"=>40  ), str_replace("<br/>", "\n", $keagenan_kapal));?>
        </td>
        <td>&nbsp;</td>
        
        <td>&nbsp;&nbsp;&nbsp;</td>
        <td>Remarks</td>
        <td>:</td>
        <td>
            <?php echo form_textarea(array("name" => "remarks", "id" => "remarks", "class" => "input_text", "rows"=>4,"cols"=>40  ), str_replace("<br/>", "\n", $remarks));?>
        </td>
    </tr>
    <tr>
        <td>Direktur Penyewa</td>
        <td>:</td>
        <td><?php if ($approved_by == "") $approved_by = "...................."; echo form_input(array("name" => "approved_by", "id" => "approved_by" ,"class" => "input_text", "size"=>"50"), $approved_by);?></td>
        <td>&nbsp;</td>
        <td>&nbsp;&nbsp;&nbsp;</td>          
        <td>Direktur Pemilik</td>
        <td>:</td>
        <td><?php echo form_input(array("name" => "penerbit_perjanjian", "id" => "penerbit_perjanjian", "size"=>"50"), $penerbit_perjanjian);?></td>        
    </tr>
    
</table>
<br class="bersih" />
<div id="box-control-save">
    <?=form_submit(array("name" => "btnSubmit", "class"=>"CommandButton"), "Save")?>&nbsp;
    <?=form_button(array("class"=>"CommandButton"), "Cancel", "onclick='close_gb();'")?>
</div>
<?=form_close()?>

</div>
</div>
</div>

<?php } else { ?>
<script type='text/javascript'>
    function close_gb(){
        <?php echo CloseGB(true); // close greybox   ?>
    }
</script>

<div style="padding-top: 25%;">
<div class="BoxMessage">Proses Berhasil, apakah anda mau mencetak perjanjian tersebut?<br /><?php echo $error; ?></div>

<div id="box-control-save">
    <?=form_button(array("class"=>"CommandButton"), "Ya", "onclick=\"window.open('".site_url("transaksi_penyewaan/perjanjian_print/$kode")."', '_blank');".  reloadParent()."\"")?>&nbsp;
    <?=form_button(array("class"=>"CommandButton"), "Tidak", "onclick='close_gb();'")?>
</div>
</div>
<?php } ?>

